package com.bw.zzy.partal2.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateRange;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import com.bw.zzy.partal2.common.CommonResult;
import com.bw.zzy.partal2.controller.form.DeleteWorkPlanForm;
import com.bw.zzy.partal2.controller.form.InsertWorkPlanForm;
import com.bw.zzy.partal2.controller.form.SelectDoctorWorkPlanInTime;
import com.bw.zzy.partal2.service.DoctorService;
import com.bw.zzy.partal2.service.DoctorWorkPlanService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.xml.stream.events.Comment;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/doctor/work/plan")
@Tag(name = "DoctorWorkPlanController", description = "出诊管理接口")
@CrossOrigin(origins = "http://localhost:4000", allowCredentials = "true")
public class DoctorWorkPlanController {

    @Autowired
    private DoctorWorkPlanService doctorWorkPlanService;

    @Autowired
    private DoctorService doctorService;

    @PostMapping("/selectWorkPlanByTime")
    @SaCheckPermission(value = {"ROOT", "SCHEDULE:SELECT"}, mode = SaMode.OR)
    @Operation(summary = "出诊计划查询")
    public CommonResult seletWorkPlanByTime(@RequestBody @Valid SelectDoctorWorkPlanInTime form) {
        Map param = BeanUtil.beanToMap(form);

        DateRange range = DateUtil.range(new DateTime(form.getStartDate()),
                new DateTime(form.getEndDate()), DateField.DAY_OF_MONTH);
        ArrayList dateList = new ArrayList();

        range.forEach(one->{
            dateList.add(one.toDateStr());
        });

        JSONArray array = doctorWorkPlanService.selectWorkPlanByTime(param, dateList);

        dateList.clear();

        DateRange range2 = DateUtil.range(new DateTime(form.getStartDate()),
                new DateTime(form.getEndDate()), DateField.DAY_OF_MONTH);
        range2.forEach(one -> {
            //例如：03月19日（星期一）
            dateList.add(one.toString("MM月dd日") + " (" + one.dayOfWeekEnum().toChinese() + ") ");
        });
        return CommonResult.ok().put("result", array).put("dateList", dateList);
    }



    @Operation(summary = "添加出诊计划")
    @PostMapping("/insert")
    @SaCheckPermission(value = {"ROOT","SCHEDULE:INSERT"},mode = SaMode.OR)
    public CommonResult insert(@RequestBody @Valid InsertWorkPlanForm form) {

        // 参数转换
        Map param = BeanUtil.beanToMap(form);
        // 调用service 层的逻辑进项数据保存
        String result = doctorWorkPlanService.insert(param);
        return CommonResult.ok().put("result", result);
    }


    @Operation(summary = "删除出诊计划")
    @PostMapping("/deleteWorkPlan")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "SCHEDULE:DELETE"}, mode = SaMode.OR)
    public CommonResult deleteWorkPlan(@RequestBody @Valid DeleteWorkPlanForm form) {
        doctorWorkPlanService.deleteWorkPlan(form.getWorkPlanId());
        return CommonResult.ok();
    }
}
